package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.Wa_UserMSG_ListBean;
import mamikeji.liaowa.utils.ForLog;
import mamikeji.liaowa.widget.slidelistview.SliderView;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class User_WaListAdapter extends BaseAdapter {
    List<Wa_UserMSG_ListBean.UserbadyListEntity> mList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;

    SliderView slideView;

    public User_WaListAdapter() {
    }

    public User_WaListAdapter(List<Wa_UserMSG_ListBean.UserbadyListEntity> mList, Context context) {
        this.mList = mList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onDeleteClick(int wa_id, int position);
    }

    public void setList(List<Wa_UserMSG_ListBean.UserbadyListEntity> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        slideView = (SliderView) convertView;
        if (slideView == null) {
            convertView = inflater.inflate(R.layout.item_usermsg_wa, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_usermsg_wa_name = (TextView) convertView.findViewById(R.id.textView_usermsg_wa_name);
            viewHolder.textView_usermsg_wa_time = (TextView) convertView.findViewById(R.id.textView_usermsg_wa_time);
            viewHolder.radioButton_usermsg_wa_yunyu = (RadioButton) convertView.findViewById(R.id.radioButton_usermsg_wa_yunyu);

            slideView = new SliderView(context);
            slideView.setContentView(convertView);
            slideView.setTag(viewHolder);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) slideView.getTag();
        }

        slideView.shrink();
        slideView.textView_delete.setText(R.string.user_notice_delete);

        final Wa_UserMSG_ListBean.UserbadyListEntity userbadyListEntity = mList.get(position);

        viewHolder.textView_usermsg_wa_name.setText(userbadyListEntity.getNickName());
        viewHolder.textView_usermsg_wa_time.setText(userbadyListEntity.getUserbobydescribe() != null ? userbadyListEntity.getUserbobydescribe() : "");
        viewHolder.radioButton_usermsg_wa_yunyu.setText(userbadyListEntity.getStatus() == 2
                ? R.string.main_chage_yangyu: R.string.main_chage_yunyu);
        viewHolder.radioButton_usermsg_wa_yunyu.setChecked(false);
        viewHolder.radioButton_usermsg_wa_yunyu.setChecked(userbadyListEntity.getStatus() == 2);
        viewHolder.radioButton_usermsg_wa_yunyu.setClickable(false);

        slideView.setOnItemClickListener(new SliderView.OnItemClickListener() {
            @Override
            public void onDeleteClick() {
                mOnItemClickListener.onDeleteClick(userbadyListEntity.getId() ,position);
            }
        });
        return slideView;
    }

    public class ViewHolder {
        TextView textView_usermsg_wa_name;
        TextView textView_usermsg_wa_time;
        RadioButton radioButton_usermsg_wa_yunyu;
    }

    public String mistiming(Long millisecond) {
        long timeStemp = 0;

        Date date = new Date(millisecond);
        timeStemp = date.getTime();

        long currentTimme = System.currentTimeMillis();
        ForLog.i("dd",timeStemp+"/"+currentTimme);
        long shijiaCha = currentTimme - timeStemp;
        ForLog.i("dd",shijiaCha+"/"+currentTimme);
        int count = 0;
        String timeString = "";
        //进行判断
        if (shijiaCha > 24 * 60 * 60 * 1000) {
            count = (int) (shijiaCha / (24 * 60 * 60 * 1000));
            if(count>31){
                int num = count/31;
                int num_0 = count%31;
                timeString =  num + "个月"+(num_0<10?"零"+num_0:num_0)+"天";
            }else {
                timeString =  count + "天";
            }
        }  else  {
            timeString =  "刚刚";
        }

        timeString = "（" + timeString + "）";

        return timeString;
    }

}
